package com.xqiapp.service.impl;

import com.xqiapp.bean.Datax;
import com.xqiapp.jsp.config.DataSourceTarget;
import com.xqiapp.entity.SysRole;
import com.xqiapp.mapper.RoleMapper;
import com.xqiapp.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by rwrwd7 on 2017/9/21.
 */
@Service
public class RoleService implements IRoleService {

    @Autowired
    private RoleMapper mapper;

    public static ThreadLocal<Long> local = new ThreadLocal<Long>(){
        @Override
        protected Long initialValue() {
            return 1000035l;
        }
    };

    @Transactional
    @DataSourceTarget
    @Override
    public void addRole(Datax datax) {
        SysRole role = new SysRole();
        Long id = local.get();
        id++;
        local.set(id);
        role.setId(local.get());
        role.setCode(""+id);
        role.setCreateTime("2017-12-12 12:12:12");
        role.setName("zhangsan");
        role.setVersion(1);
        int count =  mapper.addRole(role);

        if(count > 0 )
            System.out.println(datax);
        else
            new RuntimeException("errrorrrrr");
    }

    @Transactional
    @DataSourceTarget(routeKey = "ds1")
    @Override
    public void addRole1(Datax datax) {
        Long id = local.get();
        id++;
        local.set(id);
        SysRole role = new SysRole();
        role.setId(id);
        role.setCode(""+id);
        role.setCreateTime("2017-12-12 12:12:12");
        role.setName("zhangsan");
        role.setVersion(1);
        int count =  mapper.addRole(role);

        if(count > 0 )
            System.out.println(datax);
        else
            new RuntimeException("errrorrrrr");
    }

    @Transactional
    @DataSourceTarget(routeKey = "ds2")
    @Override
    public void addRole2(Datax datax) {

        SysRole role = new SysRole();
        Long id = local.get();
        id++;
        local.set(id);
        role.setId(id);
        role.setCode(""+id);
        role.setCreateTime("2017-12-12 12:12:12");
        role.setName("zhangsan");
        role.setVersion(1);
        int count =  mapper.addRole(role);

        addRole1(datax);
        //throw new RuntimeException("errrorrrrr");

//        if(count > 0 )
//            System.out.println(datax);
//        else
//            new RuntimeException("errrorrrrr");
    }

}
